-- ...So I realize that I completely ignored almost each and every
-- metrics-bound element this screen could use, but it's okay, right?

local t = Def.ActorFrame {};
-- A very useful table...
local eval_lines = {
	"W1",
	"W2",
	"W3",
	"W4",
	"W5",
	"Miss",
	"Held",
	"MaxCombo"
}

-- And a function to make even better use out of the table.
local function GetJLineValue(line, pl)
	if line == "Held" then
		return STATSMAN:GetCurStageStats():GetPlayerStageStats(pl):GetHoldNoteScores("HoldNoteScore_Held")
	elseif line == "MaxCombo" then
		return STATSMAN:GetCurStageStats():GetPlayerStageStats(pl):MaxCombo()
	else
		return STATSMAN:GetCurStageStats():GetPlayerStageStats(pl):GetTapNoteScores("TapNoteScore_" .. line)
	end
	return "???"
end

-- #################################################
-- That's enough functions; let's get this done.

-- Shared portion.
local mid_pane = Def.ActorFrame {
	-- Song/course banner.
	Def.Sprite {
		InitCommand=function(self)
			local target = GAMESTATE:IsCourseMode() and GAMESTATE:GetCurrentCourse() or GAMESTATE:GetCurrentSong()
			if target and target:HasBanner() then
				self:Load(target:GetBannerPath())
			else
				self:Load(THEME:GetPathG("Common fallback", "banner"))
			end
			self:scaletoclipped(256,80):x(_screen.cx):y(_screen.cy+142)
		end;
		OnCommand=cmd(visible,not GAMESTATE:IsCourseMode());
	},
	Def.Sprite {		
		InitCommand=function(self)
			local target = GAMESTATE:IsCourseMode() and GAMESTATE:GetCurrentCourse() or GAMESTATE:GetCurrentSong()
			if target and target:HasBanner() then
				self:Load(THEME:GetPathG("ScreenEvaluation", "BannerFrame"))
			end
			self:x(_screen.cx):y(_screen.cy+142)
		end;	
		OnCommand=cmd(visible,not GAMESTATE:IsCourseMode());
		};
	
}


-- Text that's slapped on top of the banner frame.
if not GAMESTATE:IsCourseMode() then
	--mid_pane[#mid_pane+1] = LoadActor(THEME:GetPathG("ScreenEvaluation", "StageDisplay")) .. {
	--	OnCommand=cmd(x,_screen.cx;y,_screen.cy-71;zoom,0.75)
	--}
else
	mid_pane[#mid_pane+1] = Def.BitmapText {
		Font="_teko semibold 36px",
		InitCommand=function(self)
			local course = GAMESTATE:GetCurrentCourse()
			self:settext(( course:GetDisplayFullTitle() ))
			self:x(_screen.cx):y(_screen.cy+170):diffuse(color("#FFFEE3")):strokecolor(color("#212324")):diffusebottomedge(color("#FFE880")):uppercase(true)
		end,
		OnCommand=cmd(playcommand,"Set";diffusealpha,0;decelerate,0.4;diffusealpha,1;zoom,0.75),
	}
end

t[#t+1] = mid_pane

-- #################################################
-- Player stats and labels 
if GAMESTATE:IsHumanPlayer(PLAYER_1) then 
	local p1evalx = SCREEN_CENTER_X-400
	local p1evaly = SCREEN_TOP+125
	local eval_offset = 44

	for i, v in ipairs(eval_lines) do
	local spacing = 24*i
	local cur_line = "JudgmentLine_" .. v
	local p_grade = STATSMAN:GetCurStageStats():GetPlayerStageStats(PLAYER_1):GetGrade()
	
	t[#t+1] = Def.ActorFrame {
		LoadActor(THEME:GetPathG("_grade", "pane")) .. {
			InitCommand=cmd(horizalign,left;x,p1evalx;y,p1evaly);
		};
		LoadActor(THEME:GetPathG("GradeDisplay", "Grade " .. p_grade)) .. {
			InitCommand=cmd(zoom,0.7;horizalign,left;x,p1evalx-3;y,p1evaly);
			OnCommand=cmd(zoomy,0.5;diffusealpha,0;sleep,0.7;decelerate,0.2;zoomy,0.7;diffusealpha,1);
		},
	};
	
	t[#t+1] = Def.ActorFrame {
		OnCommand=cmd(diffusealpha,0;sleep,0.05*i;linear,0.1;diffusealpha,1);
		LoadActor(THEME:GetPathG("_eval", "line")) .. {
			InitCommand=cmd(horizalign,left;x,p1evalx;y,p1evaly+eval_offset+spacing;);
		};
		LoadFont("Common Normal") .. {
			InitCommand=cmd(horizalign,left;zoom,0.7;diffuse,JudgmentLineToColor(cur_line);diffusebottomedge,ColorMidTone(JudgmentLineToColor(cur_line));strokecolor,color("#000000");x,p1evalx+8;y,p1evaly+eval_offset+spacing;settext,string.upper(JudgmentLineToLocalizedString(cur_line)););
		};
		LoadFont("ScoreDisplayNormal Text") .. {
			InitCommand=cmd(horizalign,right;zoom,0.7;diffuse,JudgmentLineToColor(cur_line);diffusebottomedge,ColorMidTone(JudgmentLineToColor(cur_line));x,p1evalx+190;y,p1evaly+eval_offset+spacing-4);
			OnCommand=function(self)
				self:settext(GetJLineValue(v, PLAYER_1))
			end,
		};
	};
	end;
	
		t[#t+1] = Def.ActorFrame {
		InitCommand=cmd(x,p1evalx+100;y,p1evaly+eval_offset+24*9;horizalign,right;);
		OnCommand=cmd(diffusealpha,0;sleep,0.05*9;linear,0.1;diffusealpha,1);
			LoadActor(THEME:GetPathG("_eval", "line")) .. {
			};
			LoadFont("Common Normal") .. {
				InitCommand=cmd(horizalign,left;zoom,0.7;diffuse,Color("White");diffusebottomedge,color("#CACECF");strokecolor,color("#000000");settext,"DP %";addx,-91;);
			};
			LoadFont("ScoreDisplayNormal Text") .. {
				InitCommand=cmd(horizalign,right;zoom,0.7;diffuse,Color("White");diffusebottomedge,color("#CACECF");strokecolor,color("#000000");addy,-4;addx,88);
				OnCommand=function(self)
					self:settext(FormatPercentScore(STATSMAN:GetCurStageStats():GetPlayerStageStats(PLAYER_1):GetPercentDancePoints()))
				end;
			};
		};
		t[#t+1] = Def.ActorFrame {
		InitCommand=cmd(x,p1evalx+100;y,p1evaly+eval_offset+24*10;horizalign,right;);
		OnCommand=cmd(diffusealpha,0;sleep,0.05*10;linear,0.1;diffusealpha,1);
			LoadActor(THEME:GetPathG("_small eval", "line")) .. {
			};
			LoadFont("Common Normal") .. {
				InitCommand=cmd(horizalign,left;zoom,0.5;diffuse,Color("White");diffusebottomedge,color("#CACECF");strokecolor,color("#000000");settext,"SCORE";addx,-91;addy,-3);
			};
			LoadFont("ScoreDisplayNormal Text") .. {
				InitCommand=cmd(horizalign,right;zoom,0.5;diffuse,Color("White");diffusebottomedge,color("#CACECF");strokecolor,color("#000000");addy,-6;addx,44);
				OnCommand=function(self)
					self:settext(STATSMAN:GetCurStageStats():GetPlayerStageStats(PLAYER_1):GetScore())
				end;
			};
		};
end;

if GAMESTATE:IsHumanPlayer(PLAYER_2) then 
	local p2evalx = SCREEN_CENTER_X+150+81-31
	local p2evaly = SCREEN_TOP+125
	local eval_offset = 44

	for i, v in ipairs(eval_lines) do
	local spacing = 24*i
	local cur_line = "JudgmentLine_" .. v
	local p_grade = STATSMAN:GetCurStageStats():GetPlayerStageStats(PLAYER_2):GetGrade()
	
	t[#t+1] = Def.ActorFrame {
		LoadActor(THEME:GetPathG("_grade", "pane")) .. {
			InitCommand=cmd(horizalign,left;x,p2evalx;y,p2evaly);
		};
		LoadActor(THEME:GetPathG("GradeDisplay", "Grade " .. p_grade)) .. {
			InitCommand=cmd(zoom,0.7;horizalign,left;x,p2evalx-3;y,p2evaly);
			OnCommand=cmd(zoomy,0.5;diffusealpha,0;sleep,0.7;decelerate,0.2;zoomy,0.7;diffusealpha,1);
		},
	};
	
	t[#t+1] = Def.ActorFrame {
		OnCommand=cmd(diffusealpha,0;sleep,0.05*i;linear,0.1;diffusealpha,1);
		LoadActor(THEME:GetPathG("_eval", "line")) .. {
			InitCommand=cmd(horizalign,left;x,p2evalx;y,p2evaly+eval_offset+spacing;);
		};
		LoadFont("Common Normal") .. {
			InitCommand=cmd(horizalign,left;zoom,0.7;diffuse,JudgmentLineToColor(cur_line);diffusebottomedge,ColorMidTone(JudgmentLineToColor(cur_line));strokecolor,color("#000000");x,p2evalx+8;y,p2evaly+eval_offset+spacing;settext,string.upper(JudgmentLineToLocalizedString(cur_line)););
		};
		LoadFont("ScoreDisplayNormal Text") .. {
			InitCommand=cmd(horizalign,right;zoom,0.7;diffuse,JudgmentLineToColor(cur_line);diffusebottomedge,ColorMidTone(JudgmentLineToColor(cur_line));x,p2evalx+190;y,p2evaly+eval_offset+spacing-4);
			OnCommand=function(self)
				self:settext(GetJLineValue(v, PLAYER_2))
			end,
		};
	};
	end;
	
		t[#t+1] = Def.ActorFrame {
		InitCommand=cmd(x,p2evalx+100;y,p2evaly+eval_offset+24*9;horizalign,right;);
		OnCommand=cmd(diffusealpha,0;sleep,0.05*9;linear,0.1;diffusealpha,1);
			LoadActor(THEME:GetPathG("_eval", "line")) .. {
			};
			LoadFont("Common Normal") .. {
				InitCommand=cmd(horizalign,left;zoom,0.7;diffuse,Color("White");diffusebottomedge,color("#CACECF");strokecolor,color("#000000");settext,"DP %";addx,-91;);
			};
			LoadFont("ScoreDisplayNormal Text") .. {
				InitCommand=cmd(horizalign,right;zoom,0.7;diffuse,Color("White");diffusebottomedge,color("#CACECF");strokecolor,color("#000000");addy,-4;addx,88);
				OnCommand=function(self)
					self:settext(FormatPercentScore(STATSMAN:GetCurStageStats():GetPlayerStageStats(PLAYER_2):GetPercentDancePoints()))
				end;
			};
		};
		t[#t+1] = Def.ActorFrame {
		InitCommand=cmd(x,p2evalx+100;y,p2evaly+eval_offset+24*10;horizalign,right;);
		OnCommand=cmd(diffusealpha,0;sleep,0.05*10;linear,0.1;diffusealpha,1);
			LoadActor(THEME:GetPathG("_small eval", "line")) .. {
			};
			LoadFont("Common Normal") .. {
				InitCommand=cmd(horizalign,left;zoom,0.5;diffuse,Color("White");diffusebottomedge,color("#CACECF");strokecolor,color("#000000");settext,"SCORE";addx,-91;addy,-3);
			};
			LoadFont("ScoreDisplayNormal Text") .. {
				InitCommand=cmd(horizalign,right;zoom,0.5;diffuse,Color("White");diffusebottomedge,color("#CACECF");strokecolor,color("#000000");addy,-6;addx,44);
				OnCommand=function(self)
					self:settext(STATSMAN:GetCurStageStats():GetPlayerStageStats(PLAYER_2):GetScore())
				end;
			};
		};
end;

-- if gameplay_pause_count > 0 then
	-- t[#t+1]= Def.BitmapText{
		-- Font= "_roboto condensed 24px",
		-- Text= THEME:GetString("PauseMenu", "pause_count") .. ": " .. gameplay_pause_count,
		-- InitCommand=cmd(x,SCREEN_CENTER_X;y,SCREEN_BOTTOM-100+26;);
		-- OnCommand=function(self)
			-- self:diffuse(color("#ffffff")):zoom(0.8);
			-- self:diffusealpha(0):sleep(0.5):smooth(0.3):diffusealpha(1);
		-- end;
		-- OffCommand=cmd(decelerate,0.3;diffusealpha,0);
	-- }
-- end

t[#t+1] = Def.ActorFrame {
    InitCommand=cmd(x,SCREEN_CENTER_X;y,SCREEN_CENTER_Y+80;visible,not GAMESTATE:IsCourseMode(););
    OnCommand=cmd(diffusealpha,0;zoomx,0.7;decelerate,0.3;diffusealpha,1;zoomx,1;);
	OffCommand=cmd(decelerate,0.3;diffusealpha,0;);
	LoadActor(THEME:GetPathG("", "_stageFrame"))  .. {
	    InitCommand=cmd(diffusealpha,1;zoom,1.0);
	};

	    LoadFont("_teko light 24px") .. {
          InitCommand=cmd(zoom,0.75;maxwidth,SCREEN_WIDTH;diffuse,color("#FFFFFF");uppercase,true;horizalign,center;);
          OnCommand=cmd(queuecommand,"Set");
          ChangedLanguageDisplayMessageCommand=cmd(queuecommand,"Set");
          		SetCommand=function(self)
			local curScreen = Var "LoadingScreen";
			local curStageIndex = GAMESTATE:GetCurrentStageIndex() + 1;
			local playMode = GAMESTATE:GetPlayMode();
			local curStage = GAMESTATE:GetCurrentStage();
			if GAMESTATE:IsCourseMode() then
				local stats = STATSMAN:GetCurStageStats()
				if not stats then
					return
				end
				local mpStats = stats:GetPlayerStageStats( GAMESTATE:GetMasterPlayerNumber() )
				local songsPlayed = mpStats:GetSongsPassed() + 1
				self:settextf("%i / %i", songsPlayed, GAMESTATE:GetCurrentCourse():GetEstimatedNumStages());
			else
				if GAMESTATE:IsEventMode() then
					self:settextf("FREE");
				else
					self:settextf("%s", ToEnumShortString(curStage));
				end
			end;
			self:diffuse(StageToColor(curStage));
			end;
		};
};	

if STATSMAN:GetCurStageStats():OnePassed() == true then

	t[#t+1] = Def.ActorFrame {
		LoadActor("cleared") .. {
			InitCommand=cmd(Center;);
			OnCommand=cmd(diffusealpha,0;zoomy,1.7;sleep,0.2;decelerate,0.3;zoomy,1;diffusealpha,1;sleep,0.6;linear,0.3;diffusealpha,0;)
		};
		LoadActor("glow") .. {
			InitCommand=cmd(Center;diffuse,color("#B5E7F7");blend,'BlendMode_Add');
			OnCommand=cmd(diffusealpha,0;zoomy,1.7;sleep,0.2;decelerate,0.3;zoomy,1;diffusealpha,0.6;sleep,0.6;linear,0.3;diffusealpha,0;)
		};
	};

end;

return t;

